package com.et.drive.info

import com.et.logger.ELog
import com.et.logger.TAGS
import com.et.machine.convert.IMachineOperationInfo
import com.et.machine.convert.machineOperationInfo
import com.et.util.parseBoolean
import com.et.util.toBCDInt

data class MachineRunStatus(
    var  isBusiness: Int,
    var  isGateOpened: Boolean,
    var  isGameOpened: Boolean,
    var isLightSenseClosed: Boolean,
    var  isCloudBoardClosed: Boolean,
    var  isNotAcceptedBanknotes: Boolean,
    var isSendChannelInfo: Boolean
) {
    override fun toString(): String {
        return "MachineRunStatus(isBusiness=$isBusiness, isGateOpened=$isGateOpened, isGameOpened=$isGameOpened, isLightSenseClosed=$isLightSenseClosed, isCloudBoardClosed=$isCloudBoardClosed, isNotAcceptedBanknotes=$isNotAcceptedBanknotes, isSendChannelInfo=$isSendChannelInfo)"
    }
}

data class VendingMachineStatus(
    var isDriveNpResponse: Boolean,
    var isCloudBoardStuck: Boolean,
    var isLeftTemperatureSensorFault: Boolean,
    var isRightTemperatureSensorFault: Boolean,
    var isNoGoodsToPickUp: Boolean,
    var isElectricLockDoorOpened: Boolean,
    var isLightSensingFailure: Boolean,
    var isNoResponseFromReader: Boolean
) {
    override fun toString(): String {
        return "VendingMachineStatus(isDriveNpResponse=$isDriveNpResponse, isCloudBoardStuck=$isCloudBoardStuck, isLeftTemperatureSensorFault=$isLeftTemperatureSensorFault, isRightTemperatureSensorFault=$isRightTemperatureSensorFault, isNoGoodsToPickUp=$isNoGoodsToPickUp, isElectricLockDoorOpened=$isElectricLockDoorOpened, isLightSensingFailure=$isLightSensingFailure, isNoResponseFromReader=$isNoResponseFromReader)"
    }
}

data class BanknotesStatus(
    var isNoResponseFromBanknotesDevice: Boolean,
    var isMotorFailure: Boolean,
    var isBanknotesSensorFault: Boolean,
    var isVerificationError: Boolean,
    var isMoneyBoxTakenAway: Boolean,
    var isMoneyBoxFull: Boolean,
    var isBanknoteHolderBlocked: Boolean,
    var ignored: Boolean
) {
    override fun toString(): String {
        return "BanknotesStatus(isNoResponseFromBanknotesDevice=$isNoResponseFromBanknotesDevice, isMotorFailure=$isMotorFailure, isBanknotesSensorFault=$isBanknotesSensorFault, isVerificationError=$isVerificationError, isMoneyBoxTakenAway=$isMoneyBoxTakenAway, isMoneyBoxFull=$isMoneyBoxFull, isBanknoteHolderBlocked=$isBanknoteHolderBlocked)"
    }
}

data class CoinsStatus(
    var isNoResponseFromCoinsDevice: Boolean,
    var isPowerLow: Boolean,
    var isCoinsSensorFault: Boolean,
    var isVerificationError: Boolean,
    var isBanknoteHolderInBlocked: Boolean,
    var isBanknoteHolderBackBlocked: Boolean,
    var isFiftyCentsMisCurrency: Boolean,
    var isOneYuanMisCurrency: Boolean

) {
    override fun toString(): String {
        return "CoinsStatus(isNoResponseFromCoinsDevice=$isNoResponseFromCoinsDevice, isPowerLow=$isPowerLow, isCoinsSensorFault=$isCoinsSensorFault, isVerificationError=$isVerificationError, isBanknoteHolderInBlocked=$isBanknoteHolderInBlocked, isBanknoteHolderBackBlocked=$isBanknoteHolderBackBlocked, isFiftyCentsMisCurrency=$isFiftyCentsMisCurrency, isOneYuanMisCurrency=$isOneYuanMisCurrency)"
    }
}

data class CoinsNumber(
    var FiftyCentsNumber: Int,
    var OneYuanNumber: Int,
    var BanknotesNumber: Int
) {
    override fun toString(): String {
        return "CoinsNumber(FiftyCentsNumber='$FiftyCentsNumber', OneYuanNumber='$OneYuanNumber', BanknotesNumber='$BanknotesNumber')"
    }
}

data class MachineOperationInfo(
    var MachineRunStatus: MachineRunStatus,
    var VendingMachineStatus: VendingMachineStatus,
    var BanknotesStatus: BanknotesStatus,
    var CoinsStatus: CoinsStatus,
    var CoinsNumber: CoinsNumber
) {
    override fun toString(): String {
        return "MachineOperationInfo(\n" +
                "MachineRunStatus = $MachineRunStatus\n" +
                "VendingMachineStatus = $VendingMachineStatus\n" +
                "BanknotesStatus = $BanknotesStatus\n" +
                "CoinsStatus = $CoinsStatus\n" +
                "CoinsNumber = $CoinsNumber\n" +
                ")"
    }
}


object IMachineOperationInfoImpl : IMachineOperationInfo {
    private var callback: ((MachineOperationInfo) -> Unit?)? = null
    override fun subscribe(_callback: (MachineOperationInfo) -> Unit) {
        callback = _callback
    }

    fun machineOperationInfoNotify(byteArray: ByteArray) {
        callback?.let {
            it(
                MachineOperationInfo(
                    MachineRunStatus = MachineRunStatus(
                        byteArray[0].toInt(),
                        byteArray[0].parseBoolean(1),
                        byteArray[0].parseBoolean(3),
                        byteArray[0].parseBoolean(4),
                        byteArray[0].parseBoolean(5),
                        byteArray[0].parseBoolean(6),
                        byteArray[0].parseBoolean(7),
                    ),
                    VendingMachineStatus = VendingMachineStatus(
                        byteArray[1].parseBoolean(0),
                        byteArray[1].parseBoolean(1),
                        byteArray[1].parseBoolean(2),
                        byteArray[1].parseBoolean(3),
                        byteArray[1].parseBoolean(4),
                        byteArray[1].parseBoolean(5),
                        byteArray[1].parseBoolean(6),
                        byteArray[1].parseBoolean(7),
                    ),
                    BanknotesStatus = BanknotesStatus(
                        byteArray[2].parseBoolean(0),
                        byteArray[2].parseBoolean(1),
                        byteArray[2].parseBoolean(2),
                        byteArray[2].parseBoolean(3),
                        byteArray[2].parseBoolean(4),
                        byteArray[2].parseBoolean(5),
                        byteArray[2].parseBoolean(6),
                        byteArray[2].parseBoolean(7),
                    ),
                    CoinsStatus = CoinsStatus(
                        byteArray[3].parseBoolean(0),
                        byteArray[3].parseBoolean(1),
                        byteArray[3].parseBoolean(2),
                        byteArray[3].parseBoolean(3),
                        byteArray[3].parseBoolean(4),
                        byteArray[3].parseBoolean(5),
                        byteArray[3].parseBoolean(6),
                        byteArray[3].parseBoolean(7),
                    ),
                    CoinsNumber = CoinsNumber(
                        byteArray.toBCDInt(4, 2),
                        byteArray.toBCDInt(6, 2),
                        byteArray.toBCDInt(8, 2),
                    )
                ).also {
                    machineOperationInfo = it
                    ELog.d(TAGS.machine, "Machine Operation Info $it")
                }
            )
        }
    }
}


